package me.zhengjie.config;

import java.sql.Timestamp;
import org.springframework.core.convert.converter.Converter;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class StringToTimestampConverter implements Converter<String, Timestamp> {
    private final SimpleDateFormat dateFormat;

    public StringToTimestampConverter(String dateFormatPattern) {
        this.dateFormat = new SimpleDateFormat(dateFormatPattern);
    }

    @Override
    public Timestamp convert(String source) {
        try {
            return new Timestamp(dateFormat.parse(source).getTime());
        } catch (ParseException e) {
            throw new IllegalArgumentException("Invalid date format. Please use the format: yyyy-MM-dd HH:mm:ss");
        }
    }
}
